# Poppler integration as subproject
# This will compile the minimal Poppler code needed for PDF processing

# Define Poppler source directories
set(POPPLER_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/poppler")
set(POPPLER_CPP_DIR "${POPPLER_SOURCE_DIR}/cpp")
set(POPPLER_CORE_DIR "${POPPLER_SOURCE_DIR}/poppler")
set(POPPLER_GOO_DIR "${POPPLER_SOURCE_DIR}/goo")
set(POPPLER_FOFI_DIR "${POPPLER_SOURCE_DIR}/fofi")
set(POPPLER_SPLASH_DIR "${POPPLER_SOURCE_DIR}/splash")

# Check if Poppler source exists
if(NOT EXISTS ${POPPLER_SOURCE_DIR}/CMakeLists.txt)
    message(FATAL_ERROR "Poppler source not found at ${POPPLER_SOURCE_DIR}")
endif()

# Create minimal Poppler configuration
set(POPPLER_VERSION "25.09.90")
set(PACKAGE_VERSION "${POPPLER_VERSION}")

# Configure Poppler config header (simplified)
set(POPPLER_CONFIG_CONTENT 
"#ifndef POPPLER_CONFIG_H
#define POPPLER_CONFIG_H

#define PACKAGE_VERSION \"${POPPLER_VERSION}\"
#define POPPLER_VERSION \"${POPPLER_VERSION}\"

// Basic Windows/MinGW configuration
#ifdef _WIN32
#define HAVE_WINDOWS_H 1
#define WIN32_LEAN_AND_MEAN 1
#endif

// Enable minimal features needed for our use case
#define HAVE_SPLASH 1
#define SPLASH_CMYK 1
#define USE_FIXEDPOINT 1

// Disable features we don't need
#undef HAVE_CAIRO
#undef HAVE_FONTCONFIG
#undef USE_CMS
#undef ENABLE_LIBOPENJPEG
#undef ENABLE_LIBPNG
#undef ENABLE_LIBTIFF
#undef ENABLE_ZLIB
#undef ENABLE_NSS3

#endif /* POPPLER_CONFIG_H */
")

# Write config header
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/poppler_config.h" "${POPPLER_CONFIG_CONTENT}")

# Collect minimal Poppler source files (core functionality only)
set(POPPLER_SOURCES
    # Goo (basic utilities)
    ${POPPLER_GOO_DIR}/gfile.cc
    ${POPPLER_GOO_DIR}/gmem.cc
    ${POPPLER_GOO_DIR}/GooString.cc
    ${POPPLER_GOO_DIR}/gmempp.cc
    ${POPPLER_GOO_DIR}/gstrtod.cc
    ${POPPLER_GOO_DIR}/GooTimer.cc
    ${POPPLER_GOO_DIR}/NetPBMWriter.cc
    ${POPPLER_GOO_DIR}/PNGWriter.cc
    ${POPPLER_GOO_DIR}/JpegWriter.cc
    ${POPPLER_GOO_DIR}/TiffWriter.cc
    ${POPPLER_GOO_DIR}/ImgWriter.cc
    
    # Fofi (font)
    ${POPPLER_FOFI_DIR}/FoFiBase.cc
    ${POPPLER_FOFI_DIR}/FoFiEncodings.cc
    ${POPPLER_FOFI_DIR}/FoFiTrueType.cc
    ${POPPLER_FOFI_DIR}/FoFiType1.cc
    ${POPPLER_FOFI_DIR}/FoFiType1C.cc
    
    # Splash (rendering)
    ${POPPLER_SPLASH_DIR}/Splash.cc
    ${POPPLER_SPLASH_DIR}/SplashBitmap.cc
    ${POPPLER_SPLASH_DIR}/SplashClip.cc
    ${POPPLER_SPLASH_DIR}/SplashFTFont.cc
    ${POPPLER_SPLASH_DIR}/SplashFTFontEngine.cc
    ${POPPLER_SPLASH_DIR}/SplashFTFontFile.cc
    ${POPPLER_SPLASH_DIR}/SplashFont.cc
    ${POPPLER_SPLASH_DIR}/SplashFontEngine.cc
    ${POPPLER_SPLASH_DIR}/SplashFontFile.cc
    ${POPPLER_SPLASH_DIR}/SplashFontFileID.cc
    ${POPPLER_SPLASH_DIR}/SplashPath.cc
    ${POPPLER_SPLASH_DIR}/SplashPattern.cc
    ${POPPLER_SPLASH_DIR}/SplashScreen.cc
    ${POPPLER_SPLASH_DIR}/SplashState.cc
    ${POPPLER_SPLASH_DIR}/SplashXPath.cc
    ${POPPLER_SPLASH_DIR}/SplashXPathScanner.cc
    
    # Core Poppler (essential PDF parsing)
    ${POPPLER_CORE_DIR}/Annot.cc
    ${POPPLER_CORE_DIR}/Array.cc
    ${POPPLER_CORE_DIR}/BuiltinFont.cc
    ${POPPLER_CORE_DIR}/BuiltinFontTables.cc
    ${POPPLER_CORE_DIR}/Catalog.cc
    ${POPPLER_CORE_DIR}/CharCodeToUnicode.cc
    ${POPPLER_CORE_DIR}/CMap.cc
    ${POPPLER_CORE_DIR}/Decrypt.cc
    ${POPPLER_CORE_DIR}/Dict.cc
    ${POPPLER_CORE_DIR}/Error.cc
    ${POPPLER_CORE_DIR}/FileSpec.cc
    ${POPPLER_CORE_DIR}/FlateStream.cc
    ${POPPLER_CORE_DIR}/Function.cc
    ${POPPLER_CORE_DIR}/Gfx.cc
    ${POPPLER_CORE_DIR}/GfxFont.cc
    ${POPPLER_CORE_DIR}/GfxState.cc
    ${POPPLER_CORE_DIR}/GlobalParams.cc
    ${POPPLER_CORE_DIR}/JArithmeticDecoder.cc
    ${POPPLER_CORE_DIR}/JBIG2Stream.cc
    ${POPPLER_CORE_DIR}/JPXStream.cc
    ${POPPLER_CORE_DIR}/Lexer.cc
    ${POPPLER_CORE_DIR}/Link.cc
    ${POPPLER_CORE_DIR}/NameToCharCode.cc
    ${POPPLER_CORE_DIR}/Object.cc
    ${POPPLER_CORE_DIR}/Outline.cc
    ${POPPLER_CORE_DIR}/OutputDev.cc
    ${POPPLER_CORE_DIR}/Page.cc
    ${POPPLER_CORE_DIR}/PageTransition.cc
    ${POPPLER_CORE_DIR}/Parser.cc
    ${POPPLER_CORE_DIR}/PDFDoc.cc
    ${POPPLER_CORE_DIR}/PDFDocEncoding.cc
    ${POPPLER_CORE_DIR}/PDFDocFactory.cc
    ${POPPLER_CORE_DIR}/ProfileData.cc
    ${POPPLER_CORE_DIR}/PSTokenizer.cc
    ${POPPLER_CORE_DIR}/SplashOutputDev.cc
    ${POPPLER_CORE_DIR}/Stream.cc
    ${POPPLER_CORE_DIR}/UnicodeMap.cc
    ${POPPLER_CORE_DIR}/UnicodeTypeTable.cc
    ${POPPLER_CORE_DIR}/XRef.cc
    
    # CPP Binding (what we actually use)
    ${POPPLER_CPP_DIR}/poppler-document.cpp
    ${POPPLER_CPP_DIR}/poppler-page.cpp
    ${POPPLER_CPP_DIR}/poppler-page-renderer.cpp
    ${POPPLER_CPP_DIR}/poppler-image.cpp
    ${POPPLER_CPP_DIR}/poppler-global.cpp
    ${POPPLER_CPP_DIR}/poppler-private.cpp
    ${POPPLER_CPP_DIR}/poppler-rectangle.cpp
    ${POPPLER_CPP_DIR}/poppler-version.cpp
)

# Create Poppler static library
add_library(poppler_minimal STATIC ${POPPLER_SOURCES})

# Set include directories
target_include_directories(poppler_minimal PUBLIC
    ${CMAKE_CURRENT_BINARY_DIR}  # For config header
    ${POPPLER_SOURCE_DIR}
    ${POPPLER_CORE_DIR}
    ${POPPLER_GOO_DIR}
    ${POPPLER_FOFI_DIR}
    ${POPPLER_SPLASH_DIR}
    ${POPPLER_CPP_DIR}
)

# Set compile definitions
target_compile_definitions(poppler_minimal PRIVATE
    BUILDING_POPPLER=1
    HAVE_CONFIG_H=1
)

# Configure C++ standard
set_target_properties(poppler_minimal PROPERTIES
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED ON
)

# Suppress warnings for external code
if(MSVC)
    target_compile_options(poppler_minimal PRIVATE /W0)
else()
    target_compile_options(poppler_minimal PRIVATE -w)
endif()

message(STATUS "Configured minimal Poppler integration")